package com.platform.service;

import com.platform.dao.ApiCartMapper;
import com.platform.entity.CartVo;
import com.platform.entity.GoodsSpecificationVo;
import com.platform.entity.GoodsVo;
import com.platform.entity.ProductVo;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ApiCartService {
    @Autowired
    private ApiCartMapper cartDao;
    @Autowired
    private ApiGoodsSpecificationService goodsSpecificationService;

    public CartVo queryObject(Integer id) {
        return cartDao.queryObject(id);
    }


    public List<CartVo> queryList(Map<String, Object> map) {
        return cartDao.queryList(map);
    }


    public int queryTotal(Map<String, Object> map) {
        return cartDao.queryTotal(map);
    }

    public void save(CartVo cart) {
        cartDao.save(cart);
        // 更新购物车搭配减价
        // 判断购物车中是否存在此规格商品
        Map cartParam = new HashMap();
        cartParam.put("user_id", cart.getUser_id());
        List<CartVo> cartInfoList = cartDao.queryList(cartParam);
        Map crashParam = new HashMap();
        List<Integer> goods_ids = new ArrayList();
        List<CartVo> cartUpdateList = new ArrayList();
        for (CartVo cartItem : cartInfoList) {
            if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                goods_ids.add(cartItem.getGoods_id());
            }
            if (!cartItem.getRetail_price().equals(cartItem.getRetail_product_price())) {
                cartItem.setRetail_price(cartItem.getRetail_product_price());
                cartUpdateList.add(cartItem);
            }
        }
        crashParam.put("goods_ids", goods_ids);
        for (CartVo cartItem : cartInfoList) {
            // 存在原始的
            if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                for (CartVo cartCrash : cartInfoList) {
                    if (!cartCrash.getId().equals(cartItem.getId())) {
                        cartUpdateList.add(cartItem);
                    }
                }
            }
        }
        if (null != cartUpdateList && cartUpdateList.size() > 0) {
            for (CartVo cartItem : cartUpdateList) {
                cartDao.update(cartItem);
            }
        }
    }

    public void update(CartVo cart) {
        cartDao.update(cart);
    }


    public void delete(Integer id) {
        cartDao.delete(id);
    }


    public void deleteBatch(Integer[] ids) {
        cartDao.deleteBatch(ids);
    }

    public void updateCheck(String[] productIds, Integer isChecked, Long userId) {
        cartDao.updateCheck(productIds, isChecked, userId);

        // 判断购物车中是否存在此规格商品
        Map cartParam = new HashMap();
        cartParam.put("user_id", userId);
        List<CartVo> cartInfoList = cartDao.queryList(cartParam);
        Map crashParam = new HashMap();
        List<Integer> goods_ids = new ArrayList();
        List<CartVo> cartUpdateList = new ArrayList();
        for (CartVo cartItem : cartInfoList) {
            if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                goods_ids.add(cartItem.getGoods_id());
            }
            if (!cartItem.getRetail_price().equals(cartItem.getRetail_product_price())) {
                cartItem.setRetail_price(cartItem.getRetail_product_price());
                cartUpdateList.add(cartItem);
            }
        }
        if (null != goods_ids && goods_ids.size() > 0) {
            crashParam.put("goods_ids", goods_ids);
            for (CartVo cartItem : cartInfoList) {
                // 存在原始的
                if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                    for (CartVo cartCrash : cartInfoList) {
                        if (null != cartItem.getChecked() && 1 == cartItem.getChecked() && !cartCrash.getId().equals(cartItem.getId())) {
                            cartUpdateList.add(cartCrash);
                            break;
                        }
                    }
                }
            }
        }
        if (null != cartUpdateList && cartUpdateList.size() > 0) {
            for (CartVo cartItem : cartUpdateList) {
                cartDao.update(cartItem);
            }
        }
    }

    public void deleteByProductIds(String[] productIds) {
        cartDao.deleteByProductIds(productIds);
    }

    public void deleteByUserAndProductIds(Long userId, String[] productIds) {
        cartDao.deleteByUserAndProductIds(userId, productIds);
    }

    public void deleteByCart(Long user_id, Integer session_id, Integer checked) {
        cartDao.deleteByCart(user_id, session_id, checked);
    }

    /***********************************************/
    /**
     * 构造购物车记录
     * @param goodsInfo
     * @param productInfo
     * @param number
     * @param userId
     * @return
     */
    public CartVo structureCatInfo(GoodsVo goodsInfo, ProductVo productInfo, Integer number, Long userId){
        //添加规格名和值
        String[] goodsSepcifitionValue = null;
        if (null != productInfo.getGoods_specification_ids() && productInfo.getGoods_specification_ids().length() > 0) {
            Map specificationParam = new HashMap();
            String[] idsArray = getSpecificationIdsArray(productInfo.getGoods_specification_ids());
            specificationParam.put("ids", idsArray);
            specificationParam.put("goods_id", goodsInfo.getId());
            List<GoodsSpecificationVo> specificationEntities = goodsSpecificationService.queryList(specificationParam);
            goodsSepcifitionValue = new String[specificationEntities.size()];
            for (int i = 0; i < specificationEntities.size(); i++) {
                goodsSepcifitionValue[i] = specificationEntities.get(i).getValue();
            }
        }
        CartVo cartInfo = new CartVo();

        cartInfo.setGoods_id(goodsInfo.getId());
        cartInfo.setProduct_id(productInfo.getId());
        cartInfo.setGoods_sn(productInfo.getGoods_sn());
        cartInfo.setGoods_name(goodsInfo.getName());
        cartInfo.setList_pic_url(goodsInfo.getList_pic_url());
        cartInfo.setNumber(number);
        cartInfo.setSession_id("1");
        cartInfo.setUser_id(userId);
        cartInfo.setRetail_price(productInfo.getRetail_price());
        cartInfo.setMarket_price(productInfo.getMarket_price());
        if (null != goodsSepcifitionValue) {
            cartInfo.setGoods_specifition_name_value(StringUtils.join(goodsSepcifitionValue, ";"));
        }
        cartInfo.setGoods_specifition_ids(productInfo.getGoods_specification_ids());
        cartInfo.setChecked(1);

        return cartInfo;
    }

    /**
     * 选择商品规格转换为数组
     * @param ids
     * @return
     */
    public String[] getSpecificationIdsArray(String ids){
        String[] idsArray = null;
        if (org.apache.commons.lang.StringUtils.isNotEmpty(ids)){
            String[] tempArray = ids.split("_");
            if (null != tempArray && tempArray.length > 0){
                idsArray = tempArray;
            }
        }
        return idsArray;
    }

}
